/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package ly.count.ohos.demo;

import ly.count.ohos.sdk.Countly;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.global.configuration.Configuration;
import ohos.global.resource.NotExistException;
import ohos.global.resource.ResourceManager;
import ohos.global.resource.WrongTypeException;

import java.io.IOException;
import java.util.HashMap;

/**
 * 用户详情示例页面
 *
 * @since 2021-04-13
 */
public class AbilityExampleUserDetails extends Ability {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_ability_example_user_details);
        findComponentById(ResourceTable.Id_button29).setClickedListener(this::onClickUserData01);
        findComponentById(ResourceTable.Id_button30).setClickedListener(this::onClickUserData02);
        findComponentById(ResourceTable.Id_button31).setClickedListener(this::onClickUserData03);
        findComponentById(ResourceTable.Id_button32).setClickedListener(this::onClickUserData04);
        findComponentById(ResourceTable.Id_button33).setClickedListener(this::onClickUserData05);
        Countly.sharedInstance().onStart(this);
    }

    private void onClickUserData01(Component v) {
        setUserData();
    }

    private void onClickUserData02(Component v) {
        // providing any custom key values to store with user
        HashMap<String, String> custom = new HashMap<>();
        custom.put("favoriteAnimal", "dog");

        // set multiple custom properties
        Countly.userData.setCustomUserData(custom);
        Countly.userData.save();
    }

    private void onClickUserData03(Component v) {
        // providing any custom key values to store with user
        HashMap<String, String> custom = new HashMap<>();
        custom.put("leastFavoritePet", "cat");

        // set multiple custom properties
        Countly.userData.setCustomUserData(custom);
        Countly.userData.save();
    }

    private void onClickUserData04(Component v) {
    }

    private void onClickUserData05(Component v) {
    }

    private void setUserData() {
        ResourceManager resourceManager = getResourceManager();
        String result = "";
        try {
            result = resourceManager.getElement(ResourceTable.String_user_data_report_email).getString();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NotExistException e) {
            e.printStackTrace();
        } catch (WrongTypeException e) {
            e.printStackTrace();
        }
        HashMap<String, String> data = new HashMap<>();
        data.put("name", "First name Last name");
        data.put("username", "nickname");
        data.put("email", result);
        data.put("organization", "Tester");
        data.put("phone", "+123456789");
        data.put("gender", "M");

        // provide url to picture
        // data.put("picture", "http://example.com/pictures/profile_pic.png")
        // or locally from device
        // data.put("picturePath", "/mnt/sdcard/portrait.jpg")
        data.put("byear", "1987");

        // providing any custom key values to store with user
        HashMap<String, String> custom = new HashMap<>();
        custom.put("country", "Turkey");
        custom.put("city", "Istanbul");
        custom.put("address", "My house 11");

        // set multiple custom properties
        Countly.userData.setUserData(data, custom);

        // set custom properties by one
        Countly.userData.setProperty("test", "test");

        // increment used value by 1
        Countly.userData.incrementBy("used", 1);

        // insert value to array of unique values
        Countly.userData.pushUniqueValue("type", "morning");

        // insert multiple values to same property
        Countly.userData.pushUniqueValue("skill", "fire");
        Countly.userData.pushUniqueValue("skill", "earth");

        Countly.userData.save();
    }

    @Override
    public void onStop() {
        Countly.sharedInstance().onStop();
        super.onStop();
    }

    @Override
    public void onConfigurationUpdated(Configuration newConfig) {
        super.onConfigurationUpdated(newConfig);
        Countly.sharedInstance().onConfigurationChanged(newConfig);
    }
}

